<?php
/**
 * 任务管理
 * User: taiyang
 * Date: 17/12/05
 * Time: 下午15:15
 */

class Task extends MY_Controller{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 操作列表
     * name: operation
     * @return array
     * @Author:taiyang
     */
    public function action(){
        $pageTitle = "操作列表";
        $page = $this->input->get_post('page',true) ? $this->input->get_post('page',true) : 1;
        $pagesize = $this->input->get_post('pagesize',true) ? $this->input->get_post('pagesize',true) : 10;
        $like = $this->input->get_post('like',true) ? $this->input->get_post('like',true) : '';
        //列表
        $list=$this->TaskActionModel->getActionList($like,$page,$pagesize);
        //总条数
        $count=$this->TaskActionModel->getListCnt($like);
        $url = "index.php?c=task&m=action&like=".$like;
        $multipage = multipage($count, $page, $pagesize, $url);
        $this->load->view('task/action',array('pageTitle'=>$pageTitle,'list'=>$list,'multipage'=>$multipage));
    }

    /**
     * 奖励列表
     * name: prize
     * @return array
     * @Author:taiyang
     */
    public function prize(){
        $pageTitle = "奖励列表";
        $page = $this->input->get_post('page',true) ? $this->input->get_post('page',true) : 1;
        $pagesize = $this->input->get_post('pagesize',true) ? $this->input->get_post('pagesize',true) : 10;
        $like = $this->input->get_post('like',true) ? $this->input->get_post('like',true) : '';
        $category = $this->input->get_post('category',true)>-1 ? $this->input->get_post('category',true) : '-1';
        //列表
        $list=$this->TaskPrizeModel->getPrizeList($like,$page,$pagesize,$category);
        //总条数
        $count=$this->TaskPrizeModel->getListCnt($like,$category);
        $url = "index.php?c=task&m=prize&like=".$like."&category=".$category;
        $multipage = multipage($count, $page, $pagesize, $url);
        $this->load->view('task/prize',array('pageTitle'=>$pageTitle,'list'=>$list,'multipage'=>$multipage));
    }

    /**
     * 添加修改奖励
     * name: edit_prize
     * @Author:taiyang
     */
    public function edit_prize(){
        $pageTitle = "奖励配置";
        $id = $this->input->get_post('id',true) ? $this->input->get_post('id',true) : '';
        if($_REQUEST["submits"]){
            $param = $this->input->get_post("label");
            if(!empty($id)){
                $this->TaskPrizeModel->update_data($param, array("id" => $id));
            }else{
                $this->TaskPrizeModel->insert_one($param);
            }
            showMessage("index.php?c=task&m=prize");
        }
        //详细内容
        $res=$this->TaskPrizeModel->get_id($id);
        $this->load->view('task/edit_prize',array('pageTitle'=>$pageTitle,'res'=>$res));
    }
    /**
     * 添加修改任务
     * name: edit_prize
     * @Author:taiyang
     */
    public function edit_task(){
        $pageTitle = "任务配置";
        $id = $this->input->get_post('id',true) ? $this->input->get_post('id',true) : '';
        //详细内容
        if(!empty($id)){
            $res=$this->TaskModel->get_id($id);
        }
//        print_r($res);exit;
        if($_REQUEST["submits"]){
            $param = $this->input->get_post("label");
            $prize_p = $this->input->get_post("prize");
            $p_arr=json_decode($prize_p['item'],true);
            $time = $this->input->get_post("time");
            if(!empty($param['start_time'])){
                $param['start_time']=strtotime($param['start_time'].' 00:00:00');
            }else{
                $param['start_time']='';
            }
            $param['when_long']=$time['d']*86400+$time['h']*3600;

//            print_r($p_arr);exit;
            if(!empty($id)){
                $this->TaskModel->update_data($param, array("id" => $id));
            }else{
                $insert_id=$this->TaskModel->insert_one($param);
                $id=$insert_id;
            }
//            echo $id;exit;
            //新建奖品数据
            $prize_integral=$this->input->get_post('prize_integral');//后台输入的积分奖励数量
            if(!empty($prize_integral)){
                //写入奖励表
                $prize_data=array('category'=>'0','name'=>($prize_integral>=0?"+":'').$prize_integral,'prize_data'=>$prize_integral);
                //查询指定奖励是否存在
                $prize_res=$this->TaskPrizeModel->query_one(array('category'=>0,'prize_data'=>$prize_integral));
                if(empty($prize_res)){
                    $prize_id=$this->TaskPrizeModel->insert_one($prize_data);
                }else{
                    $prize_id=$prize_res['id'];
                }
                if(!empty($prize_id)){
                    $p_arr[]=array("id"=>$prize_id);
                }
            }
            //写入奖品
            $this->TaskPrizeMapModel->del_insert_prize($id,$p_arr);
            showMessage("index.php?c=task&m=task");
        }
        //取出所有操作
        $action=$this->TaskActionModel->getActionList('',0);
        //取出所有奖励
        $prize=$this->TaskPrizeModel->getPrizeList('',0,0,-2);
        //取出本任务的奖品
        $task_prize=array();
        if(!empty($id)){
            $task_prize=$this->TaskPrizeMapModel->getPrizeList($id);
        }
        //所有父级任务
        $master_task=$this->TaskModel->getTaskList('',0,0,-1,0);


        $this->load->view('task/edit_task',array('pageTitle'=>$pageTitle,'res'=>$res,'action'=>$action,'master_task'=>$master_task,'prize'=>$prize,'task_prize'=>encode_json($task_prize)));
    }

    /**
     * 任务列表
     * name: task
     * @return array
     * @Author:taiyang
     */
    public function task(){
        $pageTitle = "任务列表";
        $page = $this->input->get_post('page',true) ? $this->input->get_post('page',true) : 1;
        $pagesize = $this->input->get_post('pagesize',true) ? $this->input->get_post('pagesize',true) : 80;
        $like = $this->input->get_post('like',true) ? $this->input->get_post('like',true) : '';
        $category = $this->input->get_post('category',true) ;
        if($category != 0 && $category !=1){
            $category='-1';
        }
        //列表
        $list=$this->TaskModel->getTaskList($like,$page,$pagesize,$category);
        //总条数
        $count=$this->TaskModel->getListCnt($like,$category);
        //取出所有操作
        $action=$this->TaskActionModel->getActionList('',0,0,1);
        $url = "index.php?c=task&m=task&like=".$like."&category=".$category;
        $multipage = multipage($count, $page, $pagesize, $url);
        $this->load->view('task/task',array('pageTitle'=>$pageTitle,'list'=>$list,'multipage'=>$multipage,'action'=>$action,'category'=>$category,'like'=>$like));
    }





    /**
     * 奖励商品列表
     * name: prize
     * @return array
     * @Author:taiyang
     */
    public function goods(){
        $pageTitle = "奖励商品列表";
        $page = $this->input->get_post('page',true) ? $this->input->get_post('page',true) : 1;
        $pagesize = $this->input->get_post('pagesize',true) ? $this->input->get_post('pagesize',true) : 10;
        $like = $this->input->get_post('like',true) ? $this->input->get_post('like',true) : '';
        $category = $this->input->get_post('category',true)>-1 ? $this->input->get_post('category',true) : '-1';
        //列表
        $list=$this->TaskModel->getGoodsList($like,$page,$pagesize,$category);
        //总条数
        $count=$this->TaskModel->getGoodsListCnt($like,$category);
        $url = "index.php?c=task&m=action&like=".$like."&category=".$category;
        $multipage = multipage($count, $page, $pagesize, $url);
        $this->load->view('task/goods',array('pageTitle'=>$pageTitle,'list'=>$list,'multipage'=>$multipage));
    }

    /**
     * 添加修改奖励
     * name: edit_prize
     * @Author:taiyang
     */
    public function edit_goods(){
        $pageTitle = "领奖状态管理";
        $id = $this->input->get_post('id',true) ? $this->input->get_post('id',true) : '';
        $status = $this->input->get_post('status',true) ? $this->input->get_post('status',true) : '';
        if($_REQUEST["submits"]){
            if(!empty($id)){
                //修改表
                $this->UserTaskPrizeModel->update_data(array('status'=>$status), array("id" => $id));
                showMessage("index.php?c=task&m=goods");
            }
        }
        //详细内容
        $res=$this->TaskModel->getGoodsId($id);
        $this->load->view('task/edit_goods',array('pageTitle'=>$pageTitle,'res'=>$res));
    }

}